package com.Sep_Twenty_Seven;

/**
 * 面试题 01.02. 判定是否互为字符重排
 * 给定两个字符串 s1 和 s2，请编写一个程序，确定其中一个字符串的字符重新排列后，能否变成另一个字符串
 * https://leetcode.cn/problems/check-permutation-lcci/
 *
 * 2022/09/27 16:52
 */

public class Solution01_02 {
    public boolean CheckPermutation(String s1, String s2) {
        int n=s1.length(),m=s2.length();
        if (n!=m) return false;
        int[] nums=new int[256];
        int sum=0;
        for (int i=0;i<n;i++)
        {
            if (++nums[s1.charAt(i)]==1) sum++;
            if (--nums[s2.charAt(i)]==0) sum--;
        }
        return sum==0;
    }

    public static void main(String[] args) {
        Solution01_02 solution01_02=new Solution01_02();
        System.out.println(solution01_02.CheckPermutation("abc","bca"));
    }
}
